<?php
if (!defined('ACCESS')) {
    exit('Access denied.');
}

require dirname(__DIR__) . "/../../env.php";

class AdminSysConfig extends Base
{
    // 表名
    private static $table_name = 'yg_shop_config';
    // 查询字段
    private static $columns = array('id', 'title', 'kewords', 'description', 'countdown', 'tongji_code', 'footer_company', 'footer_copyright');


    public static function getTableName()
    {
        return self::$table_name;
    }


    public static function getInfo()
    {
        $db = self::__instance();
        $sql = "select * from " . self::getTableName() . " where id = 1";
        $ret = $db->query($sql)->fetch();
        return $ret;
    }


    public static function add($data)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        $db = self::__instance();

        $id = $db->insert(self::getTableName(), $data);
        return $id;
    }

    public static function replace($value, $id)
    {

        try {
            $db = self::__instance();
            if (!is_array($value)) {
                return false;
            }

            if (isset($value['title'])) {
                $s1 = "'" . $value['title'] . "'";
            } else {
                $s1 = "''";
            }

            if (isset($value['kewords'])) {
                $s2 = "'" . $value['kewords'] . "'";
            } else {
                $s2 = "''";
            }

            if (isset($value['description'])) {
                $s3 = "'" . $value['description'] . "'";
            } else {
                $s3 = "''";
            }


            if (isset($value['footer_company'])) {
                $s4 = "'" . $value['footer_company'] . "'";
            } else {
                $s4 = "''";
            }

            if (isset($value['tj_code'])) {
                $s5 = "'" . $value['tj_code'] . "'";
            } else {
                $s5 = "''";
            }

            if (isset($value['footer_copyright'])) {
                $s6 = "'" . $value['footer_copyright'] . "'";
            } else {
                $s6 = "''";
            }


//            $s2 = "'" . $value[1] . "'";
//            $s3 = "'" . $value[2] . "'";
//            $s4 = "'" . $value[3] . "'";
//            $s5 = "'" . $value[4] . "'";
//            $s6 = "'" . $value[5] . "'";
//            $s7 = "'" . $value[6] . "'";
//            $s8 = "'" . $value[7] . "'";

            $value_str_s = '(' . $id . ',' . $s1 . ',' . $s2 . ',' . $s3 . ',' . $s4 . ',' . $s5 . ',' . $s6 . ')';


            $sql = "REPLACE INTO yg_shop_config (`id`,`title`,`kewords`,`description`,`footer_company`,`tongji_code`,`footer_copyright`) VALUES " . $value_str_s;

            $rt = $db->exec($sql);
            return $rt;
        } catch (Exception $e) {
            $e->getMessage();
        }
    }
}